package com.dajie.pt.service;

import com.dajie.pt.service.utils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.dajie.pt.service.beans.MessageData;

import java.sql.*;
import java.util.*;

/**
 * Created by yunbinan on 15-6-11.
 */
public class SendMessageNotEmail {
    private static Logger logger = LoggerFactory.getLogger(SendMessageNotEmail.class);
    private Connection bssConnection = null;
    public static final int TPL_NOTEMAIL = 106;

    public SendMessageNotEmail() {
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:cobar://cobarvip:12001/DB_BSS?autoCommit=true&autoReconnect=true&useUnicode=true&tinyInt1isBit=false&zeroDateTimeBehavior=round&characterEncoding=UTF-8&yearIsDateType=false&zeroDateTimeBehavior=convertToNull";
        String user = "platformol";
        String password = "platformol-3";
      /*  String url = "jdbc:mysql://172.20.10.1:3324/BSS?autoCommit=true&autoReconnect=true&useUnicode=true&tinyInt1isBit=false&zeroDateTimeBehavior=round&characterEncoding=UTF-8&yearIsDateType=false&zeroDateTimeBehavior=convertToNull";
        String user = "dj_t";
        String password = "da@jie.123";*/
        try {
            Class.forName(driver);
            bssConnection = DriverManager.getConnection(url, user, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void closeConn() {
        try {
            bssConnection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public static int getCounts(String entityLists, String entityTypeList) {
        Set<String> count = new HashSet<String>();
        String[] entityList = entityLists.split(",");
        int i = 0;
        for (String entity : entityTypeList.split(",")) {
            if (entity.equals("job")) {
                count.add(CommonUtil.getCidByJid(entityList[i]));
            } else if (entity.equals("project")) {
                count.add(CommonUtil.getCidByPid(Integer.valueOf(entityList[i])));
            } else {
                count.add(CommonUtil.getCidBySid(Integer.valueOf(entityList[i])));
            }
            i++;
        }
        return count.size();

    }

    public List<MessageData> getSendMessageData() {
        List<MessageData> sendMessageData = new ArrayList();
        String sql = "select uid,GROUP_CONCAT(entity_id) AS 'entity_id',GROUP_CONCAT(entity_type) AS 'entity_type' from tb_sms_invitation where entity_type!='job' group by uid;";
        try {
            Statement statement = bssConnection.createStatement();
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {
                MessageData MessageData = new MessageData(rs.getInt("uid"), rs.getString("entity_id"), rs.getString("entity_type"));
                sendMessageData.add(MessageData);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return sendMessageData;
    }

    public static void main(String[] args) {
        SendMessageNotEmail sendMessageNotEmail = new SendMessageNotEmail();
        List<MessageData> messageDataLists = sendMessageNotEmail.getSendMessageData();
        SendData sendData = new SendData(messageDataLists);
        logger.info("main --> send counts : {}", sendData.getSendDataSize());
        (new SmsSend(sendData)).testSend();
        for (int i = 0; i < 10; i++) {
            new Thread(new SmsSend(sendData)).start();
        }
    }

}
